package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.common.enums.ParamEnum;
import com.qk.common.exception.CommonException;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.CustomerQueryDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import com.qk.vo.customer.CustomerVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author kaoM1Jy
 * @date 2025/10/16 15:25
 * @description:
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {


    @Override
    public void updateCustomer(Customer dto) {
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        this.updateById(customer);
    }

    @Override
    public CustomerVO getCustomerById(Integer id) {
        Customer customer = this.getById(id);
        CustomerVO customerVO = BeanUtil.copyProperties(customer, CustomerVO.class);
        return customerVO;
    }

    @Override
    public void addCustomer(CustomerDTO dto) {
        if(ObjectUtil.isEmpty(dto.getName())||ObjectUtil.isEmpty(dto.getPhone())){
            CommonException.throwCommonException(ParamEnum.PARAM_ERROR);
        }
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.save(customer);
    }

    @Override
    public PageResult<CustomerDO> getCustomerList(CustomerQueryDTO dto) {
        Page pages = new Page<>(dto.getPage(), dto.getPageSize());
        Page<CustomerDO> page = this.baseMapper.getCustomerList(pages, dto);
        return PageResult.<CustomerDO>builder()
                .total(Long.valueOf(pages.getTotal()).intValue())
                .rows(page.getRecords())
                .build();
    }
}
